package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        cart.setUserId(userId);
        //1、根据前端传过来的参数进行查询（dishId+dishFlavor  or  setmealId）
        List<ShoppingCart> cartList = this.shoppingCartMapper.queryByParmas(cart);

        //2、判断是否有结果集
        if(ObjectUtils.isNotEmpty(cartList)){
            //2.1、如果有，直接数量+1，进行修改
            final ShoppingCart shoppingCart = cartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            this.shoppingCartMapper.updateNumberById(shoppingCart);
        }else{
            //2.2、如果没有，组装参数，进行新增
            cart.setNumber(1);
            if(ObjectUtils.isNotEmpty(cart.getDishId())){
                final DishVO dishVO = dishMapper.queryById(cart.getDishId());
                cart.setName(dishVO.getName());
                cart.setImage(dishVO.getImage());
                cart.setAmount(dishVO.getPrice());
            }else if(ObjectUtils.isNotEmpty(cart.getSetmealId())){
                final SetmealVO setmealVO = setmealMapper.queryById(cart.getSetmealId());
                cart.setName(setmealVO.getName());
                cart.setImage(setmealVO.getImage());
                cart.setAmount(setmealVO.getPrice());
            }
            this.shoppingCartMapper.save(cart);
        }
    }

    /**
     * 删改购物车
     * @param shoppingCartDTO
     */
    @Override
    public void remove(ShoppingCartDTO shoppingCartDTO) {
        // 组装对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断菜品数量是否大于等于1
        List<ShoppingCart> cartList = shoppingCartMapper.queryByParmas(shoppingCart);
        ShoppingCart cart = cartList.get(0);
        Integer number = cart.getNumber();
        if (number > 1){
            cart.setNumber(number - 1);
            shoppingCartMapper.updateNumberById(cart);
            return;
        }

        shoppingCartMapper.remove(cart.getId());
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        final ShoppingCart cart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> cartList = this.shoppingCartMapper.queryByParmas(cart);
        return cartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        this.shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}
